package banca.online.tcp;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;

public class TcpShooter {

	static String IP = "10.239.3.13";
	static String PORT = "8992";
	static String SAMPLE_FILE = "d:\\059100.stream";

	public static void main(String[] args) {

		String stream = GetTextFromFile(SAMPLE_FILE);

		TcpShooter tester = new TcpShooter();

		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		DataOutputStream dout = new DataOutputStream(bout);

		StringBuffer sb = new StringBuffer();

		try {
			sb.append(getFixLenStr(stream.length(), stream));
			dout.writeBytes(sb.toString());
			dout.flush();
		} catch (IOException ie) {
			ie.printStackTrace();
			return;
		}

		byte[] inStream = bout.toByteArray();

		System.out.println("\n");
		System.out.println("###################### IN #######################");
		System.out.println(new String(inStream));
		System.out.println("###################### IN #######################");

		byte[] outStream = tester.fixLenStream(inStream);

		System.out.println("\n\n\n\n\n");
		System.out
				.println("###################### OUT #######################");
		System.out.println(new String(outStream));
		System.out
				.println("###################### OUT #######################");
	}

	private static String GetTextFromFile(String fileName) {
		File file = new File(fileName);
		StringBuffer contents = new StringBuffer();
		BufferedReader reader = null;

		try {
			reader = new BufferedReader(new FileReader(file));
			String text = null;

			// repeat until all lines is read
			while ((text = reader.readLine()) != null) {
				contents.append(text).append(
						System.getProperty("line.separator"));
			}
			reader.close();
			// System.out.println(contents.toString());

		} catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
			return null;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return contents.toString();

	}

	public byte[] fixLenStream(byte[] inStream) {
		DataOutputStream dout = null;
		DataInputStream din = null;
		ByteArrayOutputStream bout = null;

		java.net.Socket con = null;
		BufferedReader br = null;
		try {
			con = new java.net.Socket(IP, Integer.parseInt(PORT));
			dout = new DataOutputStream(con.getOutputStream());
			dout.write(inStream);
			System.out.println(inStream.length+" flush!");
			dout.flush();

//			din = new DataInputStream(con.getInputStream());
			br = new BufferedReader(new InputStreamReader(con.getInputStream()));
			String str = null;
			while((str = br.readLine()) != null){
				System.out.println(str);
			}
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (dout != null)
					dout.close();
				if (din != null)
					din.close();
				if (con != null)
					con.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		System.out.println("bout.toByteArray() "+bout.toByteArray());
		return bout.toByteArray();
	}

	public static String getFixLenStr(int iLen, String inStr) {
		String stReStr = inStr;

		if (iLen < inStr.length()) {
			stReStr = stReStr.substring(0, iLen);
		} else {
			for (int i = 0; i < iLen - inStr.length(); i++) {
				stReStr = stReStr + " ";
			}
		}
		return stReStr;
	}
}
